﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Data;

namespace QuanLyKhachSan.DTO
{
    public class NguoiDungDTO
    {
        // Cac thuoc tinh
        private string tenDangNhap;
        private string matKhau;
        private string hoNguoiDung;
        private string tenLot;
        private string tenNguoiDung;
        private int loaiNguoiDung;
        private string  CMND;
        private string diaChi;

        // Cac phuong thuc
        public string DiaChi
        {
            get { return diaChi; }
            set { diaChi = value; }
        }

        public string CMND1
        {
            get { return CMND; }
            set { CMND = value; }
        }

        public int LoaiNguoiDung
        {
            get { return loaiNguoiDung; }
            set { loaiNguoiDung = value; }
        }

        public string TenNguoiDung
        {
            get { return tenNguoiDung; }
            set { tenNguoiDung = value; }
        }

        public string TenLot
        {
            get { return tenLot; }
            set { tenLot = value; }
        }

        public string HoNguoiDung
        {
            get { return hoNguoiDung; }
            set { hoNguoiDung = value; }
        }

        public string MatKhau
        {
            get { return matKhau; }
            set { matKhau = value; }
        }

        public string TenDangNhap
        {
            get { return tenDangNhap; }
            set { tenDangNhap = value; }
        }

        // Các hàm kiểm tra dữ liệu
        // Kiemt tra ho ten
        static public bool KiemTraHoTen(string chuoi)
        {
            if (string.IsNullOrEmpty(chuoi ) == false)
            {
                // Ho ten khong la so
                double kq;
                if (double.TryParse(chuoi, out kq) == true)
                    return false;
                // Kiem tra giua 2 chu chi co khoang trang
                if (chuoi.IndexOf("  ") != -1)
                    return false;
                int i;
                // Kiem tra co ki tu ~ hay khong
                for (i = 0; i < chuoi.Length; i++)
                {
                    if (chuoi[i] == '~')
                        return false;
                }
                // Kiem tra cac ky tu dau phai ky tu hoa
                string[] arr = chuoi.Split(' ');

                for (i = 0; i < arr.Length; i++)
                {
                    if (string.IsNullOrEmpty(arr[i])==false)
                    {
                        if (char.IsLower(arr[i][0]) == true)
                            return false;
                    }
                }
            }
            return true  ;
        }
        
        // Kiem tra dia chi
        static public bool KiemTraDiaChi(string diaChi)
        {
            if (string.IsNullOrEmpty(diaChi ) == false)
            {
                // Kiem tra giua 2 chu chi co khoang trang
                if (diaChi.IndexOf("  ") != -1)
                    return false;
                // Kiem tra cac ky tu dau phai ky tu hoa
                string[] arr = diaChi.Split(' ');
                int i;
                for (i = 0; i < arr.Length; i++)
                {
                    if (string.IsNullOrEmpty(arr[i]) == false)
                    {
                        if (char.IsLower(arr[i][0]) == true)
                            return false;
                    }
                }
            }
            return true  ;
        }
        // Kiem tra cmnd
        static public bool KiemTraCMND(string cmnd)
        {
            if (string.IsNullOrEmpty(cmnd ) == false)
            {
                // Kiem tra cmnd co dung 9 so hay khong
                if (cmnd.Length != 9)
                    return false;

                //Kiem tra cac ki tu phai la chu so
                int i;
                for (i = 0; i < cmnd.Length; i++)
                {
                    if (char.IsDigit(cmnd[i]) == false)
                        return false;
                }
            }
            return true  ;
        }
        //ham ma hoa chuoi sinput theo MD5
        public static string MaHoaMD5(string sinput)
        {
            // chuyển chuỗi ký tự thành mảng số nguyên
            byte[] bytes = Encoding.UTF8.GetBytes(sinput);
            // tạo lớp MD5 của NET
            MD5 md5 = new MD5CryptoServiceProvider();
            // thực thi phần mã hóa => mảng byte đã băm
            byte[] hash = md5.ComputeHash(bytes);
            // chuyển thành chuỗi ký tự
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hash)
            {
                sb.Append(string.Format("{0:X2}", b));
            }

            return sb.ToString();
        }

        //Khoi tao nguoi dung tu 1 datatable
        public NguoiDungDTO(DataTable dt)
        {
            DataRow dr = dt.Rows[0];

            this.tenDangNhap = dr["tenDangNhap"].ToString();
            this.hoNguoiDung = dr["hoNguoiDung"].ToString();
            this.matKhau = dr["matKhau"].ToString();
            this.tenLot = dr["tenLot"].ToString();
            this.tenNguoiDung = dr["tenNguoiDung"].ToString();
            this.loaiNguoiDung = int.Parse(dr["loaiNguoiDung"].ToString());
            this.CMND = dr["CMND"].ToString();
            this.diaChi = dr["diaChi"].ToString();
        }
        //Khoi tao nguoi dung tu
        public NguoiDungDTO()
        {
            this.tenDangNhap = "";
            this.hoNguoiDung = "";
            this.matKhau = "";
            this.tenLot = "";
            this.tenNguoiDung = "";
            this.loaiNguoiDung = 0;
            this.CMND = "";
            this.diaChi = "";
        }
        public NguoiDungDTO(string taiKhoan,string pass,string tenHo,string tenDem,string ten,int loaiND,string cmnd,string diaChiND )
        {
            this.tenDangNhap = taiKhoan;
            this.hoNguoiDung = tenHo;
            this.matKhau = pass;
            this.tenLot = tenDem;
            this.tenNguoiDung = ten;
            this.loaiNguoiDung = loaiND;
            this.CMND = cmnd;
            this.diaChi = diaChiND;
        }
    }
}
